package com.springboot.hiresystem.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.hiresystem.domain.Resumes;
import com.springboot.hiresystem.domain.Users;
import com.springboot.hiresystem.dto.req.ResumeQueryRequestDTO;
import com.springboot.hiresystem.dto.req.ResumeUpdateRequestDTO;
import com.springboot.hiresystem.dto.resp.PageResponse;
import com.springboot.hiresystem.dto.resp.Result;
import com.springboot.hiresystem.dto.resp.ResumeResponseDTO;
import com.springboot.hiresystem.service.ResumesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/resumes")
@Slf4j
public class ResumesController {

    private final ResumesService resumesService;

    public ResumesController(ResumesService resumesService) {
        this.resumesService = resumesService;
    }

    // 分页查询简历
    @GetMapping("/list")
    public Result<PageResponse<ResumeResponseDTO>> list(ResumeQueryRequestDTO request) {
        QueryWrapper<Resumes> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(request.getResumeTitle() != null, "resume_title", request.getResumeTitle());
        if (request.getUserId() != null) {
            queryWrapper.eq("user_id", request.getUserId());
        }
        queryWrapper.orderByAsc("resume_id");

        Page<Resumes> page = new Page<>(request.getPage(), request.getSize());
        Page<Resumes> resultPage = resumesService.page(page, queryWrapper);

        List<ResumeResponseDTO> dtoList = BeanUtil.copyToList(resultPage.getRecords(), ResumeResponseDTO.class);

        PageResponse<ResumeResponseDTO> response = new PageResponse<>();
        response.setTotal(resultPage.getTotal());
        response.setList(dtoList);

        return Result.success(response);
    }

    // 条件查询单个/多个简历
    @PostMapping("/query")
    public Result<List<ResumeResponseDTO>> query(@RequestBody ResumeQueryRequestDTO request) {
        QueryWrapper<Resumes> queryWrapper = new QueryWrapper<>();

        if (request.getUserId() != null) {
            queryWrapper.eq("user_id", request.getUserId());
        }
        if (request.getResumeTitle() != null) {
            queryWrapper.like("resume_title", request.getResumeTitle());
        }
        queryWrapper.orderByAsc("resume_id");

        List<Resumes> resumeList = resumesService.list(queryWrapper);
        List<ResumeResponseDTO> dtoList = BeanUtil.copyToList(resumeList, ResumeResponseDTO.class);

        return Result.success(dtoList);
    }

    // 新增或更新简历
    @PostMapping("/update")
    public Result<String> update(@RequestBody ResumeUpdateRequestDTO request) {
        Resumes resume = BeanUtil.copyProperties(request, Resumes.class);
        int resumeId = resume.getResumeId() != null ? resume.getResumeId() : -1; // 默认值根据业务设定
        resume.setResumeFileUrl("F:/a learning specidic floder/homework/hire-system/src/main/resources/static/upload/"+request.getResumeFileUrl());
        if (resumeId > 0) {
            // 更新
            resumesService.updateById(resume);
            return Result.success("更新成功");
        } else {
            // 设置默认字段
            resume.setCreatedAt(new Date());
            resume.setIsDefault(0); // 默认非默认简历
            if (!resumesService.save(resume)) {
                return Result.failure(500, "添加失败");
            }
            return Result.success("新增成功");
        }
    }

    // 删除简历
    @PostMapping("/delete")
    public Result<String> delete(@RequestBody Resumes resume) {
        resumesService.removeById(resume.getResumeId());
        return Result.success("删除成功");
    }

    //根据id查询,返回数组
    @PostMapping("/getResumeByUserId")
    public Result<List<ResumeResponseDTO>> getResumeById(@RequestBody Users user) {
        QueryWrapper<Resumes> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", user.getUserId());
        List<Resumes> resumeList = resumesService.list(queryWrapper);
        List<ResumeResponseDTO> dtoList = BeanUtil.copyToList(resumeList, ResumeResponseDTO.class);
        return Result.success(dtoList);
    }
    //根据简历id查询
    @PostMapping("/getResumeByResumeId")
    public Result<ResumeResponseDTO> getResumeById(@RequestBody Resumes resume) {
        Resumes resumeById = resumesService.getById(resume.getResumeId());
        ResumeResponseDTO dto = BeanUtil.copyProperties(resumeById, ResumeResponseDTO.class);
        return Result.success(dto);
    }

    //根据简历id更新默认状态
    @PostMapping("/updateDefault")
    public Result<Resumes> updateDefault(@RequestBody Resumes resume) {
        //根据resume中的userId查询所有的resume
        QueryWrapper<Resumes> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", resume.getUserId());
        List<Resumes> resumeList = resumesService.list(queryWrapper);
        //排除出当前的resume
        for (Resumes res : resumeList) {
            if (res.getResumeId() != resume.getResumeId()) {
                res.setIsDefault(0);
                resumesService.updateById(res);
            }
        }
        return Result.success(resume);
    }

}
